﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.IO;

namespace SmallTalker
{
    /// <summary>
    /// Interaction logic for talker.xaml
    /// </summary>
    public partial class talker : Window
    {
        public talker()
        {
            InitializeComponent();
        }
        [StructLayout(LayoutKind.Sequential)]
        private struct MARGINS
        {
            public int cxLeftWidth;      // width of left border that retains its size
            public int cxRightWidth;     // width of right border that retains its size
            public int cyTopHeight;      // height of top border that retains its size
            public int cyBottomHeight;   // height of bottom border that retains its size
        };

        [DllImport("DwmApi.dll")]
        private static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS pMarInset);
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                WindowInteropHelper windowInteropHelper = new WindowInteropHelper(this);
                IntPtr myHwnd = windowInteropHelper.Handle;
                HwndSource mainWindowSrc = System.Windows.Interop.HwndSource.FromHwnd(myHwnd);

                mainWindowSrc.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0);

                MARGINS margins = new MARGINS()
                {
                    cxLeftWidth = -1,
                    cxRightWidth = -1,
                    cyBottomHeight = -1,
                    cyTopHeight = -1
                };
                DwmExtendFrameIntoClientArea(myHwnd, ref margins);
                MouseDown += delegate { DragMove(); };
            }
            catch
            {
                MessageBox.Show("Ouch... this is embaressing; SAL has just failed! plz report 2 the team 'error aero', and if ur running XP juz say 'error airxp' thnx a lot!");
                this.Close();
            }
            //start of real code
            try
            {
                if (Environment.OSVersion.Version.Major < 6) //will handle non aero-compliant Operating systems (think the 50% markt share XP still has...)
                {
                    this.Background = Brushes.Honeydew; //change this if u want
                }
                MessageBox.Show("Welcome to the SmallTalker! Currently, it will only record the sentiment of what you say :[ but we're working on the rest right now!");
            }
            catch
            {
                MessageBox.Show("error setup");
                this.Close();
            }
        }
        private int RandomNumber(int min, int max)
        {
            try
            {
                Random random = new Random();
                return random.Next(min, max);
            }
            catch
            {
                MessageBox.Show("OK, this is the LAST STRAW! I'm not gonna go around answering you stupid questions without any compensation... Srry, SAL is encountering some, echm, 'technical difficulties,' and the team would LOVE it if u could contact them and tell them 'error ran#' have fun once SAL comes to ;P");
                this.Close();
                return 0;
            }
        }
        public string hapout()
        {
            string[] towork = textBox1.Text.Split(' ');
            StreamReader hapword = new StreamReader("happywords.txt");
            StreamReader sadword = new StreamReader("sadwords.txt");
            string hapwords = hapword.ReadToEnd();
            string sadwords = sadword.ReadToEnd();
            hapword.Close();
            sadword.Close();
            string end = "";
            foreach (string i in towork)
            {
                if (sadwords.ToLower().Contains(i))
                {
                    if (hapwords.ToLower().Contains(i))
                    {
                        end = end + i + " ";
                    }
                    else
                    {
                        end = end + "<!ALERT!~sad txt~!ALERT!>" + " ";
                    }
                }
                else
                {
                    end = end + i + " ";
                }
            }
            return end;
        }
        public string sadout()
        {
            string[] towork = textBox1.Text.Split(' ');
            StreamReader sadword = new StreamReader("sadwords.txt");
            StreamReader hapword = new StreamReader("hapwords.txt");
            string hapwords = hapword.ReadToEnd();
            string sadwords = sadword.ReadToEnd();
            hapword.Close();
            sadword.Close();
            string end = "";
            foreach (string i in towork)
            {
                if (hapwords.ToLower().Contains(i))
                {
                    if (sadwords.ToLower().Contains(i))
                    {
                        end = end + i + " ";
                    }
                    else
                    {
                        end = end + "<!ALERT!~happy txt~!ALERT!>" + " ";
                    }
                }
                else
                {
                    end = end + i + " ";
                }
            }
            return end;
        }
        public string neutout()
        {
            string[] towork = textBox1.Text.Split(' ');
            StreamReader neutword = new StreamReader("neutralwords.txt");
            StreamReader hapword = new StreamReader("hapwords.txt");
            StreamReader sadword = new StreamReader("hapwords.txt");
            string neutwords = neutword.ReadToEnd();
            string hapwords = hapword.ReadToEnd();
            string sadwords = sadword.ReadToEnd();
            hapword.Close();
            sadword.Close();
            neutword.Close();
            string end = "";
            foreach (string i in towork)
            {
                if (hapwords.ToLower().Contains(i) || sadwords.ToLower().Contains(i))
                {
                    if (neutwords.ToLower().Contains(i))
                    {
                        end = end + i + " ";
                    }
                    else
                    {
                        end = end + "<!ALERT!~happy/sad txt~!ALERT!>" + " ";
                    }
                }
                else
                {
                    end = end + i + " ";
                }
            }
            return end;
        }

        public int sadchecker(string xy)
        {
            string[] x = xy.Split(' ');
            int yx = 0;
            int howsad = 0;
            StreamReader hapsent = new StreamReader("sadsent.txt");
            if (hapsent.ReadToEnd().ToLower().Contains(xy))
            {
                howsad = 1000000;
            }
            hapsent.Close();
            StreamReader file2 = new StreamReader("sadwords.txt", true);
            foreach (string i in x)
            {
                if (file2.ReadToEnd().ToLower().Contains(x[yx]))
                {
                    howsad++;
                }
            }
            file2.Close();
            return howsad;
        }
        public int happychecker(string xy)
        {
            string[] x = xy.Split(' ');
            int yx = 0;
            int howhappy = 0;
            StreamReader file2 = new StreamReader("happywords.txt");
            StreamReader hapsent = new StreamReader("happysent.txt");
            if (hapsent.ReadToEnd().Contains(xy))
            {
                howhappy = 1000000;
            }
            hapsent.Close();
            foreach (string i in x)
            {
                if (file2.ReadToEnd().ToLower().Contains(x[yx]))
                {
                    howhappy++;
                }
            }
            file2.Close();
            return howhappy;
        }
        public int neutralchecker(string xy)
        {
            string[] x = xy.Split(' ');
            int howneutral = 0;
            StreamReader hapsent = new StreamReader("neutsent.txt");
            if (hapsent.ReadToEnd().Contains(xy))
            {
                howneutral = 1000000;
            }
            hapsent.Close();
            int yx = 0;
            StreamReader file2 = new StreamReader("neutralwords.txt");
            foreach (string i in x)
            {
                if (file2.ReadToEnd().Contains(x[yx]))
                {
                    howneutral++;
                }
            }
            file2.Close();
            return howneutral;
        }
        public void happyadder()
        {
            string[] x = textBox1.Text.Split(' ');
            StreamWriter xer = new StreamWriter("happysent.txt", true);
            xer.WriteLine(textBox1.Text);
            xer.Close();
            StreamWriter xerx = new StreamWriter("happysenttm.txt", true);
            xerx.WriteLine(DateTime.Now.ToString());
            xerx.Close();
            int yx = 0;
            foreach (string i in x)
            {
                StreamReader file2 = new StreamReader("happywords.txt");
                if (file2.ReadToEnd().ToLower().Contains(x[yx]))
                {
                    file2.Close();
                }
                else
                {
                    file2.Close();
                    StreamWriter file = new StreamWriter("happywords.txt", true);
                    file.WriteLine(x[yx].ToLower());
                    StreamWriter filex = new StreamWriter("happywordstm.txt", true);
                    filex.WriteLine(DateTime.Now.ToString());
                    filex.Close();
                    yx++;
                    file.Close();
                }
            }
        }
        public void sadadder()
        {
            string[] x = textBox1.Text.Split(' ');
            StreamWriter xer = new StreamWriter("sadsent.txt", true);
            StreamWriter filexy = new StreamWriter("sadsenttm.txt", true);
            filexy.WriteLine(DateTime.Now.ToString());
            filexy.Close();
            xer.WriteLine(textBox1.Text);
            xer.Close();
            int yx = 0;
            foreach (string i in x)
            {
                StreamReader file2 = new StreamReader("sadwords.txt");
                if (file2.ReadToEnd().ToLower().Contains(x[yx]))
                {
                    file2.Close();
                }
                else
                {
                    file2.Close();
                    StreamWriter file = new StreamWriter("sadwords.txt", true);
                    file.WriteLine(x[yx].ToLower());
                    StreamWriter filex = new StreamWriter("sadwordstm.txt", true);
                    filex.WriteLine(DateTime.Now.ToString());
                    filex.Close();
                    yx++;
                    file.Close();
                }
            }
        }
        public void neutadder()
        {
            string[] x = textBox1.Text.Split(' ');
            StreamWriter xer = new StreamWriter("neutsent.txt", true);
            xer.WriteLine(textBox1.Text);
            xer.Close();
            StreamWriter filexy = new StreamWriter("neutsenttm.txt", true);
            filexy.WriteLine(DateTime.Now.ToString());
            filexy.Close();
            int yx = 0;
            foreach (string i in x)
            {

                StreamReader file2 = new StreamReader("neutralwords.txt");
                if (file2.ReadToEnd().ToLower().Contains(x[yx]))
                {
                    file2.Close();
                }
                else
                {
                    file2.Close();
                    StreamWriter file = new StreamWriter("neutralwords.txt", true);
                    file.WriteLine(x[yx].ToLower());
                    StreamWriter filex = new StreamWriter("neutralwordstm.txt", true);
                    filex.WriteLine(DateTime.Now.ToString());
                    filex.Close();
                    yx++;
                    file.Close();
                }
            }
        }
        public string answer()
        {
            ///extract the data, say "how old" and "Nikola Tesla"
            ///or "I love" and "Bing" with some grammar rule,
            ///maybe prepositional phrases [i'm not that good on
            ///grammar :>)]then proccess it with a bunch of APIs
            ///(W|A, TrueKnowledge, Bing Instant Answer,
            ///etc.) and get the answer
            return "IDK";
        }
        public void loginqa()
        {

        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //as in, "1+1 = 4" or "Bing is awesome" it recognises fact input
            if (textBox1.Text.Contains("is") || textBox1.Text.Contains("="))
            {
                loginqa();
            }
            ///do something with
            ///analyzer to figure out if it's
            ///sad/happy/neut + then use a proccess to determine
            ///the answer and then use sadout/neutout/hapout
            ///to 'personalize' it.
            textBox1.Text = "";
        }
        public int analyzer()
        {
            int s = sadchecker(textBox1.Text);
            int n = neutralchecker(textBox1.Text);
            int h = happychecker(textBox1.Text);
            if (s > n & s > h)
            {
                if (h / s > .6 || h / n > .6)
                {
                    sadadder();
                }
                return 0;
            }
            else if (n > s & n > h)
            {
                if (h / n > .6 || s / n > .6)
                {
                    neutadder();
                }
                return 1;
            }
            else if (h > s & h > n)
            {
                if (s / h > .6 || n / h > .6)
                {
                    happyadder();
                }
                return 2;
            }
            else
            {
                return 3;
            }
        }
    }
}
